<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>添加分类</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="shortcut icon" th:href="@{/icon/favicon.ico}">
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
</head>
<body>
<form class="layui-form layui-form-pane"  lay-filter="component-form-group">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">新闻分类</label>
            <div class="layui-input-inline">
                <input type="text" name="title" lay-verify="title" autocomplete="off"
                       placeholder="请输入新的分类"
                       class="layui-input" id="sorts">
            </div>
        </div>
    </div>
    <a class="layui-btn " id="newSort" style="margin-left: 130px">添加分类</a>
</form>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script>
<script th:src="@{/layuiadmin/layui/layui.js}"></script>
<script>
    layui.use(['form','layer'],function () {
        var $ = layui.$
        ,layer = layui.layer;
        const button = $('#newSort');
        button.click(function () {
            const str = $('#sorts').val();
            if (str.length === 0){
                layer.msg('请先输入一个分类');
            }
            $.ajax({
                type: 'GET',
                url: '/admin/addOneType/'+str,
                data:{},
                success: function (data) {
                    let json;
                    if ((typeof data === 'string') && data.constructor === String){
                        if (data.charAt(0) === '<'){
                            layer.msg('服务器错误',{
                                icon: 5
                            });
                            return;
                        }
                        json = JSON.parse(data);
                    }else{
                        json = data;
                    }
                    if (json.code === 1){
                        layer.msg('添加成功',{icon: 6,time: 500},function () {
                            let id = json.data[0].id;
                            let typeName = json.data[0].typeName;
                            parent.$('#thisSelect02').append($('<option>', {value:id, text:typeName}));
                        });
                    }else if (json.code === 2){
                        layer.msg(json.msg,{
                            icon: 5
                        });
                    }
                },
                error: function () {
                    layer.msg('服务器异常');
                }
            });
        });
    });
</script>
</body>
</html>